package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.util.ValidateCodeUtils;
import com.jk1123.sms.SmsClient;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class ValidateCodeController {

    @Autowired
    private SmsClient smsClient;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/send")
    public Result send(String type,String telephone) {
        if (StringUtils.isNotBlank(telephone)&& type.equals("ORDER")) {
            String code = ValidateCodeUtils.generateValidateCode4String(4);
            log.info("验证码:{}", code);
            //smsClient.sendMessage(telephone,code);
            redisTemplate.opsForValue().set("member_loginPhone:" + telephone, code, 30, TimeUnit.SECONDS);
            return new Result(true, "验证码已发送");
        }

        if (StringUtils.isNotBlank(telephone)&& type.equals("MOBILE_SIGNIN")) {
            String code = ValidateCodeUtils.generateValidateCode4String(4);
            log.info("验证码:{}", code);
            //smsClient.sendMessage(telephone,code);
            redisTemplate.opsForValue().set("member_loginPhone:" + telephone, code, 30, TimeUnit.SECONDS);
            return new Result(true, "验证码已发送");
        }

        return new Result(false, MessageConst.ACTION_FAIL);
    }

}

